package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.work.distribute.DistributeTimeoutBservice;
import com.zmn.oms.common.constant.DistributeTimeoutConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.timeout.DistributeTimeoutDTO;
import com.zmn.oms.model.dto.work.distribute.MqWaitDistributeDTO;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsDistributeWorkBService;
import com.zmn.track.common.constant.TrackConfigConsts;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 工单待派单队列
* @author: LuJia
* @version: v1.0
* @since: 2020/3/8 17:00
**/
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class OrderDistributeListener {

    @Autowired
    private ZsDistributeWorkBService distributeWorkBService;
    @Autowired
    private DistributeTimeoutBservice distributeTimeoutBservice;

	@AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_WAIT_DISTRIBUTE)
	public void distribute(String message) {

		log.info("#oms#distribute# 派单消费[{}]", message);
		if (StringUtil.isBlank(message)) {
			return;
		}

        distributeWorkBService.distributeConsume(JSON.parseObject(message, MqWaitDistributeDTO.class));
    }

    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_DISTRIBUTE_TIMEOUT_CHECK)
    public void checkDistributeTimeout(String message) {
        log.info("#oms#distribute# 派单超时检查消费[{}]", message);
        if (StringUtil.isBlank(message)) {
            return;
        }
        try {
            DistributeTimeoutDTO distributeTimeoutDTO = JSON.parseObject(message, DistributeTimeoutDTO.class);
            distributeTimeoutBservice.timeoutCheck(distributeTimeoutDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_DISTRIBUTE_TIMEOUT_DEAL_CHECK)
    public void checkDistributeTimeoutDeal(String message) {
        log.info("#oms#distribute# 派单超时检查处理消费[{}]", message);
        if (StringUtil.isBlank(message)) {
            return;
        }
        try {
            DistributeTimeoutDTO distributeTimeoutDTO = JSON.parseObject(message, DistributeTimeoutDTO.class);
            distributeTimeoutDTO.setReasonId(TrackConfigConsts.TRACK_REASON_ID_SYSTEM_WAIT);
            distributeTimeoutDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
            distributeTimeoutDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            distributeTimeoutDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            distributeTimeoutDTO.setOperatorLogRemark("系统默认等待");
            distributeTimeoutBservice.timeoutProcess(distributeTimeoutDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
